package com.example.HelloSpace;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.example.HelloSpace.utils.ToastShow;
import com.google.android.material.navigation.NavigationView;

public class EncyclopediaFragment extends Fragment  {
    private DrawerLayout drawerLayout;
    private NavigationView navigationView;

    public EncyclopediaFragment() {
        // Required empty public constructor
    }

    public static EncyclopediaFragment newInstance(String param1, String param2) {
        EncyclopediaFragment fragment = new EncyclopediaFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_encyclopedia, container, false);

        drawerLayout = view.findViewById(R.id.drawer_layout);
        navigationView = view.findViewById(R.id.nav_view);
        drawerLayout.setScrimColor(Color.TRANSPARENT);
        Menu menu = navigationView.getMenu();

        // 默认隐藏子菜单


//        // 监听父项的点击事件
//        navigationView.setNavigationItemSelectedListener(item -> {
//            int id = item.getItemId();
//            if (id == R.id.nav_gallery) {
//                // 切换子菜单的可见性
//                boolean isVisible = menu.findItem(R.id.nav_gallery_sub_1).isVisible();
//                menu.setGroupVisible(R.id.group_gallery, !isVisible);
//                return true;
//            }
//            return false;
//        });
//
//        // 设置NavigationView的Item选中监听器
//        navigationView.setNavigationItemSelectedListener(item -> {
//            handleNavigationItemSelected(item);
//            return true; // 返回true表示已经处理了此事件
//        });
//
//        // 打开Drawer并选中"首页"
        openDrawerAndSelectHome();
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            menu.setGroupVisible(R.id.group_gallery, false);
        }, 100);

        return view;
    }

    private void openDrawerAndSelectHome() {
        // 打开Drawer
        drawerLayout.openDrawer(GravityCompat.START);

        // 延迟执行，确保Drawer已经打开
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            selectHomeItem();
        }, 200); // 可能需要根据实际情况调整延迟时间
    }

    private void selectHomeItem() {
        MenuItem homeItem = navigationView.getMenu().findItem(R.id.nav_home);
        if (homeItem != null) {
            homeItem.setChecked(true);
            // 处理“首页”被选中的逻辑，但不关闭Drawer
            handleNavigationItemSelected(homeItem);

        }
    }

    private void handleNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();

        // 使用if-else if结构来处理不同的点击事件
        if (id == R.id.nav_home) {
            //ToastShow.show(requireContext(), "首页");
        } else if (id == R.id.nav_gallery) {
            //ToastShow.show(requireContext(), "图库");
        } else if (id == R.id.nav_gallery_sub_1) {
            //ToastShow.show(requireContext(), "子选项 1");
        } else if (id == R.id.nav_gallery_sub_2) {
            //ToastShow.show(requireContext(), "子选项 2");
        }
        // 注意：这里不再调用 closeDrawer 方法
    }
}